package ${package.Controller};

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PutMapping;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import com.tgpms.auth.security.TokenService;
import java.io.Serializable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 控制层
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Api(value = "${table.controllerName}", tags = "${table.comment!}相关接口")
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/ev/${entity}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

	private final Logger log = LoggerFactory.getLogger(${table.controllerName}.class);
	
	@Autowired
	private ${table.serviceName} i${table.serviceName};
	@Resource
	private TokenService tokenService;
	
	@ApiOperation(value = "新增${table.comment!}接口",notes = "新增${table.comment!}接口",httpMethod = "POST")
	@PostMapping(value = "/add${entity}Info")
	public Result<String> add${entity}Info(@RequestBody ${entity} en,HttpServletRequest request) {
		log.info("新增${table.comment!}接口接收到的参数为: "+en.toString());
		try{
			String insertName = tokenService.getUserName(request);
			en.setInsertUser(insertName);
			Result<String> result = i${table.serviceName}.add${entity}(en);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ERROR(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
	}
	
	@ApiOperation(value = "修改${table.comment!}接口",notes = "修改${table.comment!}接口",httpMethod = "PUT")
	@PutMapping(value = "/update${entity}Info")
	public Result<String> update${entity}Info(@RequestBody ${entity} en,HttpServletRequest request) {
		log.info("修改${table.comment!}接口接收到的参数为: "+en.toString());
		try{
			String insertName = tokenService.getUserName(request);
			en.setModifyUser(insertName);
			Result<String> result = i${table.serviceName}.update${entity}(en);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ERROR(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
	}
	
	@ApiOperation(value = "删除${table.comment!}接口",notes = "删除${table.comment!}接口",httpMethod = "DELETE")
	@DeleteMapping(value = "/delete${entity}Info")
	public Result<String> delete${entity}Info(@RequestParam Serializable id,HttpServletRequest request) {
		log.info("删除${table.comment!}接口接收到的参数为: {id: "+id+" }");
		try{
			Result<String> result = i${table.serviceName}.delete${entity}(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ERROR(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
	}
}
</#if>